package com.itheima.tanhua.apiImpl;




import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.tanhua.mapper.UserInfoMapper;
import com.itheima.tanhua.pojo.db.UserInfo;
import com.itheima.tanhua.api.db.UserInfoApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

//暴露这个类
@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;
    //添加
    @Override
    public void saveUserInfo(UserInfo userInfo) {
        int insert = userInfoMapper.insert(userInfo);
        System.out.println(insert);
    }

    //添加头像的路径
    @Override
    public void updateById(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 通过userId查询用户
     * @param userId
     * @return
     */
    @Override
    public UserInfo selectById(Long userId) {
       return userInfoMapper.selectById(userId);
    }

    /**
     * 展示好友列表
     * @param friendIds
     * @param keyword
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public List<UserInfo> findContactsList(List<Long> friendIds, String keyword, Integer page, Integer pagesize) {
        Page<UserInfo> ipage=new Page<>(page,pagesize);
        LambdaQueryWrapper<UserInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getId,friendIds);
        wrapper.like(StrUtil.isNotBlank(keyword),UserInfo::getNickname,keyword);
        userInfoMapper.selectPage(ipage,wrapper);
        return ipage.getRecords();
    }

    //查询所有用户并进行分页操作
    public List<UserInfo>  findAll() {
        return  userInfoMapper.selectList(null);
    }

    @Override
    public List<UserInfo> findByIds(List<Long> list) {
        return userInfoMapper.selectBatchIds(list);
    }


}
